import axios from "axios"
import { message } from "antd"
// 封装Axios 代码 
// Loading
// 操作提示 

const baseURL = "http://localhost:3800/";     // 基路径 

const AUTH_TOKEN = "";
axios.defaults.baseURL = baseURL;    // 使用反向代理就不需要使用axios基路径 
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


export function ShowLoading(msg: any) {
    msg = msg || '请求中'
    message.destroy()
    message.loading(msg)
}

export function ShowSuccess(msg: any) {
    msg = msg || '请求成功'
    message.destroy()
    message.success(msg)
}

export function ShowError(msg: any) {
    msg = msg || '请求失败'
    message.destroy()
    message.error(msg)
}
// 拦截器   Interceptors   

// 请求发送的拦截器  request
axios.interceptors.request.use(function (config: any) {
    // 成功
    // loading 

    var token = sessionStorage.getItem("autoken") ? sessionStorage.getItem("autoken") : '';
    config.headers.token = token   // 请求头 
    // console.log(config)
    ShowLoading('请求中')
    return config;
}, function (error) {
    // 失败
    ShowError("请求发送失败")
    return Promise.reject(error);
});

// 请求响应的拦截器  response
axios.interceptors.response.use(function (response) {
    // 成功 
    console.log(response.data)
    // ShowSuccess() PC端需要对应的提示
    // 成功提示还有失败提示 
    if (response.data.code == 200) {
        ShowSuccess(response.data.msg)
    } else {
        if (response.data.code == 3300) {
            // token 无效 请重新登录 
            //  Hash.push("/login")
            setTimeout(() => {
                window.location.href = "#/login"
            }, 200)

        }
        ShowError(response.data.msg)
    }

    return response;
}, function (error) {
    // 失败 
    console.log(error)
    ShowError("响应失败")
    return Promise.reject(error);
});


// axios 二次封装 
export function get(url: any, params: any, headers: any) {
    return new Promise(function (resolve, reject) {
        axios.get(url, {
            params,  // 请求参数
            headers // 请求头 
        })
            .then(res => {
                // console.log(res)
                resolve(res.data) // res.data 当做成功回调的参数返回出去 
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function post(url: any, body: any, headers: any) {
    return new Promise(function (resolve, reject) {
        // console.log(headers)
        axios.post(url, body, {
            headers // 请求头 
        })
            .then(res => {
                // console.log(res)
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function patch(url: any, params: any, headers: any) {
    return new Promise(function (resolve, reject) {
        axios({
            url,
            method: "patch",
            data: params,
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function Delete(url: any, params: any, headers: any) {
    return new Promise(function (resolve, reject) {
        axios({
            url,
            method: "delete",
            data: params,
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export {
    axios,
    baseURL
}